﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PathologyBLL.Service;
using PathologyDAL.Modle;
using PathologyDAL.Service;
using Sunny.UI;

namespace Pathology.UI
{
    public partial class RefreshFilterCondition : UIForm
    {
        public RefreshFilterCondition()
        {
            InitializeComponent();
        }

        private Filter filterCurrent;
        private readonly Action<TumourConditionDisPlay, bool> Reload;

       
        private readonly TumourConditionDisPlay eidtorFilter;

        public RefreshFilterCondition(TumourConditionDisPlay CurrentFilter, Action<TumourConditionDisPlay, bool > Reload)
        {
            InitializeComponent();
           
            this.Reload = Reload;
            this.eidtorFilter = CurrentFilter;
        }



        private async void uiComboBoxFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            uiComboBoxName.Clear();
            if (uiComboBoxFilter.SelectedIndex>-1)
            {

                Filter selectFilterType = (Filter)uiComboBoxFilter.SelectedIndex;

                ConditionManageServicels conditionManageServicels = new ConditionManageServicels((Filter)uiComboBoxFilter.SelectedIndex);
                var list = await conditionManageServicels.LoadEffictivItems(string.Empty);
                uiComboBoxName.ValueMember = "Id";
                uiComboBoxName.DisplayMember = "Name";
                uiComboBoxName.DataSource = list;
              
            
            }
            else
            {
               
                uiComboBoxName.DataSource = null;
            }
            //从数据库中查
        
           
           
        }

        private async void uiButtonOK_ClickAsync(object sender, EventArgs e)
        {
            //检查数据

            int currendFliteType = uiComboBoxFilter.SelectedIndex;
            if (currendFliteType <  0)
            {
                this.ShowErrorDialog2("请先选择类型！");
                return;
            }

            FilterCondition? selectCondition = uiComboBoxName.SelectedItem as FilterCondition;
            if (selectCondition == null)
            {
                this.ShowErrorDialog2("请填写名称");
                return;
            }

            if (uiRadioButtonGroupPossibility.SelectedIndex == -1)
            {
                this.ShowErrorDialog2("请选择概率");
                return;
            }
            TumourManageServices services = new TumourManageServices();
            Possibility possible = ( Possibility)(uiRadioButtonGroupPossibility.SelectedIndex + 1);
            Filter filter = (Filter)currendFliteType;
            if (eidtorFilter.FilterConditionInfo.Filter== filter&& selectCondition.Id== eidtorFilter.FilterConditionInfo.Id)
            {
                if (eidtorFilter.TumourCondition.Possibility == possible)
                {
                    this.ShowErrorDialog2("未进行任何修改");
                }
                else
                {
                    //改概率
                    eidtorFilter.TumourCondition.Possibility = possible;
                    await services.UpdatePossibleAsync(eidtorFilter);
                    this.ShowSuccessTip("修改概率成功！", 1500);
                    Reload.Invoke(eidtorFilter, false);
                    this.Close();

                }

            }
            else
            {

                bool possibleItemsExist = await services.CheckTumourConditionsExistAsync(eidtorFilter.TumourInfo, selectCondition);
                if (possibleItemsExist)
                {
                    this.ShowErrorDialog2("条目重复，请检查！");
                    return;
                }


                eidtorFilter.FilterConditionInfo = selectCondition;
                eidtorFilter.TumourCondition.FilterCondition=selectCondition.Id;    
                eidtorFilter.TumourCondition.Possibility= possible;
                await services.UpdatePossibleItemAsync(eidtorFilter);
               // selectCondition.Possibility = possible;
                this.ShowSuccessTip("修改成功！", 1500);
                Reload.Invoke(eidtorFilter, false);
                this.Close();

            }
           


        }

        private async void uiButton1_ClickAsync(object sender, EventArgs e)
        {

            TumourManageServices services = new TumourManageServices();
            await services.DeletePossibleItemAsync(eidtorFilter);
 
            Reload.Invoke(null , true);
            this.Close();



        }
    }
}
